package com.example.mybatisplus.service.impl;

import com.example.mybatisplus.model.domain.FilmCollection;
import com.example.mybatisplus.mapper.FilmCollectionMapper;
import com.example.mybatisplus.service.FilmCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fmx
 * @since 2024-11-17
 */
@Service
public class FilmCollectionServiceImpl extends ServiceImpl<FilmCollectionMapper, FilmCollection> implements FilmCollectionService {
    @Autowired
    private FilmCollectionMapper filmCollectionMapper;
    @Override
    public void addFilmCollection(Integer userId, Integer movieId) {
        FilmCollection filmCollection = new FilmCollection();
        filmCollection.setUserId(userId);
        filmCollection.setMovieId(movieId);

        // 调用Mapper方法插入收藏记录
        filmCollectionMapper.insertFilmCollection(filmCollection);
    }

    @Override
    public void deleteFilmCollection(Integer userId, Integer movieId) {
        int rowsAffected = filmCollectionMapper.deleteFilmCollection(userId, movieId);

        if (rowsAffected == 0) {
            // 可以记录日志或抛出异常，或者返回一个表示未找到记录的消息
        }
    }

    @Override
    public List<Integer> getCollectedMovieIdsByUserId(Integer userId) {
        return filmCollectionMapper.selectByUserId(userId);
    }

    @Override
    public boolean checkIfUserHasMovie(Integer userId, Integer movieId) {
        return filmCollectionMapper.existsUserMovie(userId, movieId);
    }
}
